package com.neusoft.tijian.service.impl;

import com.neusoft.tijian.mapper.CiReportMapper;
import com.neusoft.tijian.po.CiDetailedReport;
import com.neusoft.tijian.po.CiReport;
import com.neusoft.tijian.service.CiReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CiReportServiceImpl implements CiReportService {

    @Autowired
    private CiReportMapper ciReportMapper;

    @Override
    public List<CiReport> listCiReportByOrderId(Integer orderId) {
        // 实现查询逻辑
        return null; // 实际项目中替换为真实查询
    }

    @Override
    public Integer batchUpdateCiDetailedReport(List<CiDetailedReport> list) {
        // 实现批量更新逻辑
        return 0; // 实际项目中替换为真实更新
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createReportTemplate(Integer orderId, Integer smId) {
        // 参数校验
        if (orderId == null || smId == null) {
            return false;
        }

        try {
            // 1. 先删除该订单可能存在的旧报告数据（拆分为两个独立删除操作）
            ciReportMapper.deleteCiReportByOrderId(orderId);
            ciReportMapper.deleteCiDetailedReportByOrderId(orderId);

            // 2. 准备参数
            Map<String, Object> params = new HashMap<>();
            params.put("orderId", orderId);
            params.put("smId", smId);

            // 3. 插入ci_report表数据
            int reportCount = ciReportMapper.insertCiReport(params);

            // 4. 插入ci_detailed_report表数据
            int detailCount = ciReportMapper.insertCiDetailedReport(params);

            // 5. 两个表都插入成功才算成功
            return reportCount > 0 && detailCount > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
